﻿namespace EasyStep
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using EasyStep.Data.Entities;
using EasyStep.Business.Interfaces;


    public static class TestProjectHelper
    {

        public static TestProject CreateDefault(this ITestProjectManager testProjectManager, string projectName, Guid userId)
        {
            return testProjectManager.GetOrCreateProject(projectName, userId, () =>
                 new TestProject() { Name = projectName, Description = "automatically generated project, during registration", UserId = userId }
            );
        }


        

        public static List<TestProject> GetAvailableProjects(this ITestProjectManager testProjectManager, ITestUserManager testUserManager, ITestConstantsListManager testConstListManager=null, ITestInfoManager testInfoManager=null)
        {
            var projectsWithCList = testProjectManager.GetUserProjects(testUserManager.Current().Id).ToList();
            if (testInfoManager == null && testConstListManager == null) return projectsWithCList;
            
            projectsWithCList.ForEach(p =>
            {
                p.ConstantLists = testConstListManager == null ? p.ConstantLists : testConstListManager.GetProjectConstList(p.Id);
                p.Tests = testInfoManager==null?p.Tests :testInfoManager.GetProjectTests(p.Id);
            });
            return projectsWithCList;
        }


        public static TestProject GetById(this ITestProjectManager testProjectManager, Guid id, ITestConstantsListManager ConstListsManager = null, ITestInfoManager TestInfoManager = null)
        {
            var result = testProjectManager.Get(id);
            if (ConstListsManager != null)
                result.ConstantLists = ConstListsManager.GetProjectConstList(result.Id)
                    .OrderBy(c => c.Name);
            if (TestInfoManager!= null)
                result.Tests = TestInfoManager.GetProjectTests(result.Id)
                    .OrderBy(c => c.Name);
            return result;
        }
        
    }
}
